<template>
  <div class="role-container">
    <div class="left-wrapper">
      <!-- 列表每一项 -->
      <!--
        当前激活的下标值和每一项的下标值做匹配
        如果相等 当前项就有资格有active类名
       -->
      <div
        v-for="(item,index ) in roleList"
        :key="item.roleId"
        class="role-item"
        :class="{ active: index === currentIndex}"
        @click="switchRole(index,item.roleId)"
      >
        <div class="role-info">
          <svg-icon :icon-class="index === currentIndex ? 'user-active':'user'" class="icon" />
          {{ item.roleName }}
        </div>
        <div class="more">
          <el-dropdown>
            <span class="el-dropdown-link">
              <svg-icon icon-class="more" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <!--
                这个组件本身内部并没有抛出一个 click 事件 @e-button @click
                .native 事件修饰符 把当前点击事件自动绑定的组件内部的根元素身上
              -->
              <el-dropdown-item @click.native="editRole(item.roleId)">编辑角色</el-dropdown-item>
              <el-dropdown-item>删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
      <el-button class="addBtn" size="mini" @click="$router.push('/roleAdd')">添加角色</el-button>
    </div>
    <div class="right-wrapper">
      <!--
        tree: 要求必须传入符合规则的树状结构 [{ title:'', children:[{}]},{}]
         后端会直接返回符合规则的树状结构  / 返回的格式不能直接用(处理成树形结构)

        tree默认的识别字段  标题 - label字段  子元素 - children字段  通过props参数做自定义
        {
          children: 'childrenList'
          label: 'title'
        }

        定制
         1. 基础样式
         2. checkbox
         3. 默认就展开
       -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="功能权限" name="tree">
          <div class="tree-wrapper">
            <div v-for="item in treeList" :key="item.id" class="tree-item">
              <!-- title -->
              <div class="tree-title">{{ item.title }}</div>
              <!-- 树形结构 -->
              <el-tree
                ref="tree"
                :props="{label:'title'}"
                :data="item.children"
                show-checkbox
                :default-expand-all="true"
                node-key="id"
              />
            </div>
          </div>
        </el-tab-pane>
        <el-tab-pane :label="`成员列表(${memeberTotal})`" name="member">
          <el-table
            :data="roleUserList"
            style="width: 100%"
          >
            <el-table-column
              type="index"
              width="250"
              label="序号"
            />
            <el-table-column
              prop="name"
              label="员工姓名"
            />
            <el-table-column
              prop="userName"
              label="登录账号"
            />
          </el-table>
        </el-tab-pane>
      </el-tabs>

    </div>
  </div>
</template>

<script>
import { getRoleListAPI, getTreeListAPI, getRoleDetailAPI, getRoleUserAPI } from '@/api/role'
import { addDisabled } from '@/utils/format'
export default {
  name: 'Role',
  data() {
    return {
      roleList: [],
      currentIndex: 0,
      treeList: [],
      activeName: 'tree',
      roleUserList: [], // 成员列表
      memeberTotal: 0 // 成员总数
    }
  },
  async mounted() {
    await this.getRoleList()
    await this.getTreeList()
    // 初始化的时候使用第一项的roleId进行高亮处理
    // 这个方法调用初始化时同步调用 而它依赖的roleList treeList都是通过异步获取的
    // 为了保证数据已经准备好了 模拟同步 必须保证上面的俩个异步返回数据之后才调用方法
    this.setTreeByPerms(this.roleList[0].roleId)
    this.getRoleMemeber(this.roleList[0].roleId)
  },
  methods: {
    editRole(roleId) {
      this.$router.push({
        path: '/roleAdd',
        query: {
          roleId
        }
      })
    },
    // 根据roleId获取成员列表
    async getRoleMemeber(roleId) {
      const res = await getRoleUserAPI(roleId)
      console.log(res)
      this.roleUserList = res.data.rows
      this.memeberTotal = res.data.total
    },
    // 获取roleId对应的权限点列表 然后高亮处理tree
    async setTreeByPerms(roleId) {
      // 获取到roleId 通过roleId获取权限列表
      const res = await getRoleDetailAPI(roleId)
      console.log(res)
      // 获取tree组件的实例对象
      console.log(this.$refs.tree)
      // 遍历tree实例对象 给它们分别调用 setCheckedKeys()
      this.$refs.tree.forEach((treeInstance, index) => {
        treeInstance.setCheckedKeys(res.data.perms[index])
      })
    },
    // 记录当前点击下标值
    async switchRole(index, roleId) {
      this.currentIndex = index
      this.setTreeByPerms(roleId)
      this.getRoleMemeber(roleId)
    },
    async getRoleList() {
      const res = await getRoleListAPI()
      this.roleList = res.data
    },
    // 获取tree数据
    async getTreeList() {
      const res = await getTreeListAPI()
      this.treeList = res.data
      // 禁用
      // 目标：treeList里面的每一项以及嵌套的子项都添加一个disabled = true
      addDisabled(this.treeList)
    }
  }
}

</script>

<style scoped lang="scss">
.role-container {
  display: flex;
  font-size: 14px;
  background-color: #fff;
  padding:10px;
  .left-wrapper {
    width: 200px;
    border-right: 1px solid #e4e7ec;
    padding: 4px;
    text-align: center;

    .role-item {
      position: relative;
      height: 56px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      cursor: pointer;

      &.active {
        background: #edf5ff;
        color: #4770ff;
      }
    }

    .role-info {
      display: flex;
      align-items: center;

      .icon {
        margin-right: 10px;
      }
    }

    .more {
      display: flex;
      align-items: center;
    }
    .addBtn{
      width: 100%;
      margin-top: 20px;
    }
  }

  .right-wrapper {
    flex: 1;

    .tree-wrapper {
      display: flex;
      justify-content: space-between;

      .tree-item {
        flex: 1;
        border-right: 1px solid #e4e7ec;
        padding: 0px 4px;
        text-align: center;
        .tree-title {
          background: #f5f7fa;
          text-align: center;
          padding: 10px 0;
          margin-bottom: 12px;
        }
      }
    }

    ::v-deep .el-tabs__nav-wrap {
      padding-left: 20px;
    }

    .user-wrapper{
      padding:20px;
    }
  }
}
</style>
